; Minimum assembler file with one function call to libc.so

;;;;;;;
;  Compile with the following command line
;     nasm -f elf min.3.asm


;;;;;;;
;  Link with the following command line
;     ld -s -o min.3 min.3.o -lelf -I/lib/ld-linux.so.2

; Output:	a = 5

; Equivalent C code
;
; #include <stdio.h>
;
; int main()
; {
;   int a = 5;
;   printf("a = %d\n", a);
;   return 0;
; }

; Declare printf as external (the linker will resolve the reference)
;

	extern	printf		; the C function, to be called

section .data		; Data section, initialized variables

	a:		dd		5						; int a=5;
	fmt:	db		"a = %d", 10, 0	; The printf format, "\n", '0'

section .text			; Code section.

	global _start		; Default entry point for the linker (ld)

_start:

	push	ebp			; set up stack frame
	mov	ebp, esp

	push	dword [a]	; value of variable a
	push	dword fmt	; address of ctrl string
	call	printf		; Call C function
	add	esp, 12		; pop stack 3 push times 4 bytes (restore the stack pointer)

	mov	esp, ebp		; takedown stack frame
	pop	ebp			; same as "leave" op

	mov	eax, 1		; system call number (sys_exit)
	int	0x80
